package com.aora.sales.partner.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.aora.sales.common.bean.Company;
import com.aora.sales.common.bean.Dict;
import com.aora.sales.common.bean.Page;
import com.aora.sales.partner.dao.PartnerDao;

@Repository
public class PartnerDaoImpl implements PartnerDao {

    private SqlSession sqlSession;
    
    public SqlSession getSqlSession() {
        return sqlSession;
    }

    @Autowired
    public void setSqlSession(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }
    
    public List<Dict> getAllCategorys() {
        
        List<Dict> dictList = getSqlSession().selectList("company.getAllPartnerCategorys");
        
        if (null == dictList){
            dictList = new ArrayList<Dict>();
        }
        
        return dictList;
    }

    public List<Company> getAllPartners(Company company, Page page) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("companyName", company.getCompanyName());
        map.put("categoryId", company.getCategoryId());
        
        if (null == page){
            ////getTotalSize等于1, 则取所有符合条件的数据
            map.put("getTotalSize", 1);
        }else{
            map.put("start", page.getCurrentRecord());
            map.put("pageSize", page.getPageSize());
            map.put("getTotalSize", page.getTotalRecord());
        }
        
        
        List<Company> companyList = getSqlSession().selectList("company.getAllPartners", map);
        
        if (null == companyList){
            companyList = new ArrayList<Company>();
        }
        
        return companyList;
    }

    public void addPartner(Company company) {
        getSqlSession().insert("company.createPartner", company);
    }

    public Company getPartner(int companyId) {
        
        return getSqlSession().selectOne("company.getPartnerById", companyId);
    }

    public void updatePartner(Company company) {
        getSqlSession().update("company.updatePartner", company);
    }
}
